<template>
  <div>
    <el-form label-position="right" label-width="80px" ref="form" :rules="rules" :model="form">
      <el-form-item label="礼品名称" prop="name">
        <el-input v-model="form.name" :disabled="true" placeholder="请输入礼品名称"></el-input>
      </el-form-item>
      <el-form-item label="站点老人" prop="older_id">
        <el-select
          v-model="form.older_id"
          filterable
          placeholder="请输入老人姓名"
          :filter-method="filterMethod"
          :loading="loading"
          style="width: 100%;">
          <el-option
            v-for="item in list"
            :key="item.id"
            :label="`${item.name}(积分：${item.jifen})`"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="礼品数量" prop="num">
        <el-input v-model="form.num" type="number" placeholder="请输入礼品数量"></el-input>
      </el-form-item>
      <el-form-item label="花费积分" prop="jifen">
        <el-input v-model="form.jifen" type="number" placeholder="请输入积分"></el-input>
      </el-form-item>
      <el-form-item label="花费金额" prop="money">
        <el-input v-model="form.money" type="number" placeholder="请输入花费金额"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="msg">
        <el-input v-model="form.msg" type="textarea" placeholder="请输入备注"></el-input>
      </el-form-item>
      <el-form-item class="contacts-add">
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button @click="cancle">关闭</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import * as APIIntegral from '@/api/integral'
import * as APIDailycare from '@/api/dailycare'
import { mapState } from 'vuex'
export default {
  name: 'goods-fenpei',
  props: {
    data: {
      type: Object,
      default: () => {}
    }
  },
  computed: {
    /* ...mapState('d2admin/user', [
      'info'
    ]) */
  },
  data () {
    return {
      form: {},
      dialogVisible: false,
      searchForm: {
        page_size: 1000
      },
      list: [],
      loading: false,
      // 表单校验
      rules: {
        older_id: [
          {
            required: true,
            message: '请输入站点老人',
            trigger: 'blur'
          }
        ],
        num: [
          {
            required: true,
            message: '请输入礼品数量',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  mounted () {
    this.getOlderList()
  },
  methods: {
    // 搜索
    filterMethod (query) {
      if (query !== '') {
        this.searchForm = { ...this.searchForm, ...{ name: query } }
        this.getOlderList()
      } else {
        this.list = []
      }
    },
    // 站点列表
    getOlderList (pageObj) {
      this.loading = true
      this.searchForm = {
        ...this.searchForm,
        ...pageObj
      }
      APIDailycare.riolderList(this.searchForm).then((res) => {
        const { list = [] } = res.data;
        this.list = list || []
      }).finally(() => {
        this.loading = false
      })
    },
    // 入库
    onSubmit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          APIIntegral.rigoodsDui(this.form).then(res => {
            this.$message({ type: "success", message: "兑换成功!"});
            this.cancle()
            this.load()
          })
        }
      })
    },
    load () {
      this.$emit('load')
    },
    cancle () {
      this.$refs.form.resetFields()
      this.$emit('cancle')
    }
  },
  watch: {
    data: {
      handler (newval) {
        this.form = newval
      },
      // 可以一进页面就触发监听器  监听器自带的immediate
      immediate: true,
      deep: true
    }
  }
}
</script>

<style lang="scss">
</style>
